<?php
// +----------------------------------------------------------------------
// | CatchAdmin [Just Like ～ ]
// +----------------------------------------------------------------------
// | Copyright (c) 2017~2020 http://catchadmin.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( https://github.com/yanwenwu/catch-admin/blob/master/LICENSE.txt )
// +----------------------------------------------------------------------
// | Author: JaguarJack [ njphper@gmail.com ]
// +----------------------------------------------------------------------
namespace catchAdmin\customs\excel;

use catchAdmin\customs\model\OrderSon as OrderModel;
use catcher\library\excel\ExcelContract;
use PhpOffice\PhpSpreadsheet\Style\Alignment;

class clearOrderGoodsExport implements ExcelContract
{
    protected $orderModel;
    protected $tradeCountryData;
    protected $unitData;

    /**
     *
     * @time 2021/11/03 14:02
     * @param OrderModel $orderModel
     * @param OrderSonModel $OrderSonModel
     * @return mixed
     */
    public function __construct(OrderModel $orderModel)
    {
        $this->orderModel = $orderModel;
        //海关国家码
        $tradeCountryData = file_get_contents('json/tradeCountry.json');
        $tradeCountryData = json_decode($tradeCountryData,true);
        $this->tradeCountryData = $tradeCountryData ? $tradeCountryData:[];

        //计量单位
        $unitData = file_get_contents('json/unit.json');
        $unitData = json_decode($unitData,true);
        $this->unitData = $unitData ? $unitData:[];
    }
    /**
     * 设置头部
     *
     * @time 2020年09月08日
     * @return string[]
     */
    public function headers(): array
    {
        // TODO: Implement headers() method.
        return [
            '所属订单号', '海关备案品名', '电商平台品名', '条形码', '海关编码', '数量', '组合数量', '计量单位', '单价', '总价', '净重（kg）', '毛重（kg）', '原产国（地区）','所属清单号',
        ];

    }

    /**
     * 处理数据
     *
     * @time 2020年09月08日
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * @return \think\Collection
     */
    public function sheets()
    {
        $list = $this->orderModel->getExportList();
        foreach ($list as $key => &$value){

            $value->orderId .= "\t";
            $value->barcode .= "\t";
            $value->g_code .= "\t";
            $value->unit = $this->valueFormat($value->unit, $this->unitData);
            $value->origin_country_code = $this->valueFormat($value->origin_country_code, $this->tradeCountryData);
            $value->net_weight = (string)($value->net_weight*$value->num*$value->spec_qty);
            $value->gross_weight = (string)($value->gross_weight*$value->num*$value->spec_qty);
        }
        return $list;
    }
    /**
     * 处理数据
     * $value 值
     * $list 数组
     */
    public function valueFormat($value, $list)
    {
        foreach ($list as $k => $v){
            if($v['value'] == $value){
                $value = $v['label'];
                break;
            }
        }
        return $value;
    }



    /**
     * 设置开始行
     *
     * @time 2020年09月08日
     * @return int
     */
    public function setRow()
    {
        return 2;
    }
    /**
     * 设置行宽
     *
     * @time 2020年09月08日
     * @return int
     */
    public function setWidth()
    {
        return [
            "A"=>15, "B"=>30, "C"=>50, "D"=>20, "E"=>15, "F"=>10, "G"=>10,"H"=>10,"I"=>10,"J"=>10,"K"=>10,"L"=>10,"M"=>15,"N"=>15
        ];
    }

    /**
     * 设置标题
     *
     * @time 2020年09月08日
     * @return array
     */
    public function setTitle()
    {
        return [
            'A1:N1', '订单商品', Alignment::HORIZONTAL_CENTER
        ];
    }
}